﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Thor.Common;
using Thor.Configuration;

namespace Thor.Domain.Configuration
{
    public class AdministratorAccountHelper
    {
        #region fields

        private static ConfigurationSettings _ConfigSettings = new ConfigurationSettings();
        private static string _ApplicationNameKey = ConfigurationSettings.ApplicationNameKey;
        private static string _ApplicationNameValue = ConfigurationSettings.ApplicationNameValue;
        private static string _ConnectionStringKey = ConfigurationSettings.ConnectionStringKey;
        private static string _ConnectionStringValue = ConfigurationSettings.ConnectionStringValue;
        private static string _RepositoryAssembly = AppDomain.CurrentDomain.BaseDirectory + ConfigurationSettings.AdministratorAccountRepositoryAssembly;
        private static string _RepositoryType = ConfigurationSettings.AdministratorAccountRepositoryType;

        #endregion

        #region constructors

        private AdministratorAccountHelper() { }

        #endregion

        #region properties

        private static ConfigurationSettings ConfigurationSettings
        {
            get
            {
                if (_ConfigSettings == null)
                    _ConfigSettings = new ConfigurationSettings();

                return _ConfigSettings;
            }
        }

        #endregion

        #region methods

        private static Dictionary<string, object> GetRepositoryParameters()
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add(_ApplicationNameKey, _ApplicationNameValue);
            parameters.Add(_ConnectionStringKey, _ConnectionStringValue);
            return parameters;
        }

        private static IAdministratorAccountRepository GetRepository(Dictionary<string, object> parameters)
        {
            return AdministratorAccountManager.GetRepository(_RepositoryAssembly, _RepositoryType, parameters);
        }

        public static AdministratorAccountManager GetManager()
        {
            Dictionary<string, object> parameters = GetRepositoryParameters();
            IAdministratorAccountRepository repository = GetRepository(parameters);
            AdministratorAccountManager manager = new AdministratorAccountManager(repository);
            return manager;
        }

        public static bool IsAdministrator(string userName)
        {
            var admins = GetManager().Administrators;

            if(admins != null && admins.Count > 0)
            {
                var isAdmin = (from admin in admins
                               where admin.UserName == userName
                               select admin).Any();

                return isAdmin;
            }
            else
                return true;
        }

        #endregion
    }
}
